home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Fade Me 1.1 / source / Gamma lib ƒ / gamma.c next >
Encoding:
Text File  |  1994-10-30  |  8.1 KB  |  246 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        show init.c
  4.  
  5. Purpose:    This module handles actually putting the startup icon
  6.             on the screen at INIT loading time.
  7.             
  8. Note:        This file was not written by the author of Shutdown Fade
  9.             and is not subject to the licensing terms of the GNU General
  10.             Public License.
  11.  
  12.  
  13. Shutdown Fade -=- fade the screen to black on shutdown
  14. Copyright (C) 1993 Mark Pilgrim
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program in a file named "GNU General Public License".
  28. If not, write to the Free Software Foundation, 675 Mass Ave,
  29. Cambridge, MA 02139, USA.
  30.  
  31. \**********************************************************************/
  32.  
  33. // File "gamma.c" - Source for Altering the Gamma Tables of GDevices
  34. //   Last updated 11/9/93, MJS
  35.  
  36. // * ****************************************************************************** *
  37. //
  38. //    This is the Source Code for the Gamma Utils Library file. Use this to build
  39. //        new functionality into the library or make an A4-based library. 
  40. //    See the header file "gamma.h" for much more information. -- MJS
  41. //
  42. // * ****************************************************************************** *
  43.  
  44. #include "Traps.h"
  45. #include <GestaltEqu.h>
  46. #include <Quickdraw.h>
  47. #include <Video.h>
  48. #include "gamma.h"
  49.  
  50. long            gammaUtilsInstalled;
  51. globalGammasHdl    gammaTables;
  52.  
  53. // * ****************************************************************************** *
  54. // * ****************************************************************************** *
  55.  
  56. Boolean IsGammaAvailable() {
  57.     GDHandle theGDevice;
  58.  
  59.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  60.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  61.     
  62.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice))
  63.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  64.                 TestDeviceAttribute(theGDevice, noDriver)) return(0);
  65.  
  66.     return(-1);
  67.     }
  68.  
  69. // * ****************************************************************************** *
  70. // * ****************************************************************************** *
  71.  
  72. Boolean IsOneGammaAvailable(GDHandle theGDevice) {
  73.     
  74.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  75.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  76.     
  77.     if (TestDeviceAttribute(theGDevice, screenDevice) && 
  78.             TestDeviceAttribute(theGDevice, noDriver)) return(0);
  79.     
  80.     return(-1);
  81.     }
  82.  
  83. // * ****************************************************************************** *
  84. // * ****************************************************************************** *
  85.  
  86. OSErr SetupGammaTools() {
  87.     short errorCold=0;
  88.     globalGammasHdl tempHdl;
  89.     GammaTblPtr    masterGTable;
  90.     GDHandle theGDevice;
  91.  
  92.     if (gammaUtilsInstalled == kGammaUtilsSig) return(-1);
  93.     
  94.     gammaTables = 0;
  95.     gammaUtilsInstalled = kGammaUtilsSig;
  96.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  97.         if (errorCold = GetDevGammaTable(theGDevice, &masterGTable)) return(errorCold);
  98.         
  99.         tempHdl = (globalGammasHdl) NewHandle(sizeof(globalGammas));
  100.         if (tempHdl == 0) return(errorCold = MemError());
  101.         
  102.         (*tempHdl)->size = sizeof(GammaTbl) + masterGTable->gFormulaSize +
  103.                 (masterGTable->gChanCnt * masterGTable->gDataCnt * masterGTable->gDataWidth / 8);
  104.         (*tempHdl)->dataOffset = masterGTable->gFormulaSize;
  105.         (*tempHdl)->theGDevice = theGDevice;
  106.         
  107.         (*tempHdl)->saved = (GammaTblHandle) NewHandle((*tempHdl)->size);
  108.         if ((*tempHdl)->saved == 0) return(errorCold = MemError());
  109.         (*tempHdl)->hacked = (GammaTblHandle) NewHandle((*tempHdl)->size);
  110.         if ((*tempHdl)->hacked == 0) return(errorCold = MemError());
  111.     
  112.         BlockMove((Ptr) masterGTable, (Ptr) *(*tempHdl)->saved, (*tempHdl)->size);
  113.         
  114.         (*tempHdl)->next = gammaTables;
  115.         gammaTables = tempHdl;
  116.         }
  117.  
  118.     return(0);
  119.     }
  120.  
  121. // * ****************************************************************************** *
  122. // * ****************************************************************************** *
  123.  
  124. OSErr DoGammaFade(short percent) {
  125.     short errorCold=0;
  126.     register long size, i, theNum;
  127.     globalGammasHdl tempHdl;
  128.     unsigned char *dataPtr;
  129.  
  130.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  131.     
  132.     for(tempHdl = gammaTables; tempHdl; tempHdl = (*tempHdl)->next) {
  133.     
  134.         BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  135.         dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  136.         size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  137.         
  138.         for(i=0; i < size; i++) {
  139.             theNum = dataPtr[i];
  140.             theNum = (theNum * percent) / 100;
  141.             dataPtr[i] = theNum;
  142.             }
  143.         
  144.         if (errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked))
  145.             return(errorCold);
  146.         }
  147.         
  148.     return(0);
  149.     }
  150.  
  151. // * ****************************************************************************** *
  152. // * ****************************************************************************** *
  153.  
  154. OSErr DoOneGammaFade(GDHandle theGDevice, short percent) {
  155.     short errorCold=0;
  156.     register long size, i, theNum;
  157.     globalGammasHdl tempHdl;
  158.     unsigned char *dataPtr;
  159.  
  160.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  161.     for(tempHdl = gammaTables; tempHdl && (theGDevice != (*tempHdl)->theGDevice);
  162.             tempHdl = (*tempHdl)->next);
  163.         
  164.     BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  165.     dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  166.     size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  167.     
  168.     for(i=0; i < size; i++) {
  169.         theNum = dataPtr[i];
  170.         theNum = (theNum * percent) / 100;
  171.         dataPtr[i] = theNum;
  172.         }
  173.     
  174.     errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked);
  175.     
  176.     return(errorCold);
  177.     }
  178.  
  179. // * ****************************************************************************** *
  180. // * ****************************************************************************** *
  181.  
  182. OSErr DisposeGammaTools() {
  183.     globalGammasHdl tempHdl, nextHdl;
  184.  
  185.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  186.     for(tempHdl = gammaTables; tempHdl; tempHdl = nextHdl) {
  187.         nextHdl = (*tempHdl)->next;
  188.         DisposeHandle((Handle) (*tempHdl)->saved);
  189.         DisposeHandle((Handle) (*tempHdl)->hacked);
  190.         DisposeHandle((Handle) tempHdl);
  191.         }
  192.         
  193.     gammaUtilsInstalled = 0;
  194.     return(0);
  195.     }
  196.  
  197. // * ****************************************************************************** *
  198. // * ****************************************************************************** *
  199.  
  200. OSErr GetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  201.     short errorCold=0;
  202.     CntrlParam  *myCPB;
  203.  
  204.     ((long *) theTable)[0] = 0;
  205.  
  206.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  207.             
  208.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  209.     myCPB->csCode = cscGetGamma;
  210.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  211.     *(GammaTblPtr **) myCPB->csParam = theTable;
  212.     errorCold = PBStatus((ParmBlkPtr) myCPB, 0);
  213.  
  214.     DisposePtr((Ptr) myCPB);
  215.     return(errorCold);
  216.     }
  217.  
  218. // * ****************************************************************************** *
  219. // * ****************************************************************************** *
  220.  
  221. OSErr SetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  222.     CntrlParam *myCPB;
  223.     short errorCold=0;
  224.     CTabHandle cTab;
  225.     GDHandle saveGDevice;
  226.  
  227.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  228.  
  229.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  230.     myCPB->csCode = cscSetGamma;
  231.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  232.     *(GammaTblPtr **) myCPB->csParam = theTable;
  233.     errorCold = PBControl((ParmBlkPtr) myCPB, 0);
  234.  
  235.     if (errorCold == 0) {
  236.         saveGDevice = GetGDevice();
  237.         SetGDevice(theGDevice);
  238.          cTab = (*(*theGDevice)->gdPMap)->pmTable;
  239.         SetEntries (0, (*cTab)->ctSize, (*cTab)->ctTable);
  240.         SetGDevice(saveGDevice);
  241.         }
  242.  
  243.     DisposePtr((Ptr) myCPB);
  244.     return (errorCold);
  245.     }
  246.